#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
调试数据结构
"""

import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))

from plugins.data_analysis.data_analysis import DataSplitter, DataSaver

# 创建测试数据
test_data = [
    {"name": "张三", "department": "技术部", "salary": 15000},
    {"name": "李四", "department": "销售部", "salary": 12000},
    {"name": "王五", "department": "技术部", "salary": 18000},
    {"name": "赵六", "department": "人事部", "salary": 10000}
]

# 创建DataSplitter实例 - 测试按字段值拆分
splitter_config = {
    "split_strategy": "field_value",
    "split_field": "department",
    "output_path": "C:/temp/test_output",
    "split_output_folder": True
}
splitter = DataSplitter("data_splitter", {"config": splitter_config})

# 执行拆分
split_result = splitter.execute(test_data)
print("=== 拆分结果 ===")
print(f"完整结果: {split_result}")
print(f"拆分成功: {split_result.get('split_success', False)}")
print(f"分组数: {len(split_result.get('split_data', {}))}")
print(f"分组信息: {split_result.get('split_info', {})}")

# 创建DataSaver实例
saver_config = {
    "format": "json",  # 使用json格式
    "split_output_folder": True,
    "file_prefix": "dept_",
    "output_path": "C:/temp/test_output"  # 添加输出路径
}
saver = DataSaver("data_saver", {"config": saver_config})

# 执行保存
save_result = saver.execute(split_result)
print("\n=== 保存结果 ===")
print(f"完整保存结果: {save_result}")
print(f"保存成功: {save_result.get('saved', False)}")
print(f"文件路径: {save_result.get('file_path', 'N/A')}")
print(f"总记录数: {save_result.get('total_records', 0)}")
print(f"总分组数: {save_result.get('total_groups', 0)}")
print(f"保存文件数: {len(save_result.get('saved_files', []))}")
print(f"保存文件列表: {save_result.get('saved_files', [])}")
print(f"元数据: {save_result.get('metadata', {})}")

# 检查metadata中的saved_files
metadata = save_result.get('metadata', {})
saved_files_in_metadata = metadata.get('saved_files', [])
print(f"元数据中的保存文件数: {len(saved_files_in_metadata)}")
print(f"元数据中的保存文件列表: {saved_files_in_metadata}")